<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回匹配模式的数组条目</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.preg-filter.html">« preg_filter</a></li>
      <li style="float: right;"><a href="function.preg-last-error-msg.html">preg_last_error_msg »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.pcre.html">PCRE 函数</a></li>
    <li>返回匹配模式的数组条目</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.preg-grep" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_grep</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">preg_grep</span> &mdash; <span class="dc-title">返回匹配模式的数组条目</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.preg-grep-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>preg_grep</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$pattern</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>): <span class="type"><span class="type">array</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
  返回给定数组<code class="parameter">array</code>中与模式<code class="parameter">pattern</code>
  匹配的元素组成的数组。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.preg-grep-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">pattern</code></dt>

     <dd>

      <p class="para">
       要搜索的模式，字符串形式。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       输入数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
      如果设置为<strong><code>PREG_GREP_INVERT</code></strong>，这个函数返回输入数组中与
      给定模式<code class="parameter">pattern</code><em class="emphasis">不</em>匹配的元素组成的数组。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-grep-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
  返回使用<code class="parameter">array</code>中key做索引的数组。
   或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 errors" id="refsect1-function.preg-grep-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
如果传递的正则表达式无法正常解析，会发出 <strong><code>E_WARNING</code></strong>。 
</p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-grep-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4723">
    <p><strong>示例 #1 <span class="function"><strong>preg_grep()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;返回所有包含浮点数的元素<br /></span><span style="color: #0000BB">$fl_array&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">preg_grep</span><span style="color: #007700">(</span><span style="color: #DD0000">"/^(\d+)?\.\d+$/"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.preg-grep-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="pcre.pattern.html" class="link">PCRE 模式</a></li>
    <li class="member"><span class="function"><a href="function.preg-quote.html" class="function" rel="rdfs-seeAlso">preg_quote()</a> - 转义正则表达式字符</span></li>
    <li class="member"><span class="function"><a href="function.preg-match-all.html" class="function" rel="rdfs-seeAlso">preg_match_all()</a> - 执行一个全局正则表达式匹配</span></li>
    <li class="member"><span class="function"><a href="function.preg-filter.html" class="function" rel="rdfs-seeAlso">preg_filter()</a> - 执行一个正则表达式搜索和替换</span></li>
    <li class="member"><span class="function"><a href="function.preg-last-error.html" class="function" rel="rdfs-seeAlso">preg_last_error()</a> - 返回最后一个PCRE正则执行产生的错误代码</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>